package com.zhf.demo01.servlet;

import com.zhf.demo01.dao.UserDao;
import com.zhf.demo01.model.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/13 18:56
 */
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端界面请求的用户名和密码
        request.setCharacterEncoding("utf-8");
        String userName = request.getParameter("userName");
        System.out.println("获取到的用户名："+userName);
        String password = request.getParameter("password");
        System.out.println("获取到的密码为："+password);

        //将接受到的用户名和密码打包成对象交给DAO层处理
        User uer=new User(userName,password);
        UserDao userDao = new UserDao();
        int code = userDao.createUser(uer);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        if(code==1){
            System.out.println("注册成功");
            writer.write("<h1>注册成功</h1>");
        }else {
            System.out.println("注册失败");
            writer.write("<h1>注册失败</h1>");
        }
        writer.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
